package com.fowo.api.pda.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.model.warehouse.WarehouseItemVo;
import com.fowo.api.model.warehouse.WarehouseSearchParamPo;
import com.fowo.api.pda.service.PdaInitializeService;
import com.fowo.api.service.WarehouseService;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.user.model.CurrentUser;
import com.fowo.api.user.model.JwtUserInfo;
import com.fowo.api.user.model.LoginAccount;
import com.fowo.api.user.model.LoginResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "PDA初始化")
@RequestMapping("/api")
@RestController
public class PdaInitializeController {

    @Resource
    private WarehouseService service;
    @Resource
    private PdaInitializeService pdaInitializeService;
    @Resource
    private SysUserService sysUserService;

    @Operation(summary = "平台登录")
    @PostMapping("login/pda")
    public LoginResult account(@Validated @RequestBody LoginAccount loginAccount) {
        return pdaInitializeService.loginByAccount(loginAccount);
    }

    @Operation(summary = "取当前登录用户信息")
    @GetMapping("/pda/currentUser")
    public R<CurrentUser> currentUser(JwtUserInfo jwtUserInfo) {
        if (jwtUserInfo != null) {
            if ("ADMIN".equals(jwtUserInfo.getType())) {
                return R.data(sysUserService.getCurrentUserById(jwtUserInfo.getUserId()));
            }
        }
        return R.fail("用户未登录！");
    }

    @Operation(summary = "退出登录")
    @PostMapping("login/pda/outLogin")
    public R outLogin() {
        // 当前不做什么操作
        return R.ok(true);
    }

    @Operation(summary = "仓库列表")
    @GetMapping("/pda/warehouse")
    public R<List<WarehouseItemVo>> search(WarehouseSearchParamPo search) {
        return R.data(service.search(search));
    }
}
